package com.cr.web.controller.api;

import com.cr.business.domain.vo.QualifiedCertDetailInfoVo;
import com.cr.business.service.ITbQualifiedCertInfoService;
import com.cr.business.service.ITbUserEntityInfoService;
import com.cr.common.core.domain.AjaxResult;
import com.cr.common.exception.ServiceException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("api/h5")
@Validated
public class H5Controller {

    @Resource
    private ITbQualifiedCertInfoService tbQualifiedCertInfoService;

    @Resource
    private ITbUserEntityInfoService tbUserEntityInfoService;

    /**
     * 获取H5页面数据
     * @param certId 合格证ID
     */
    @GetMapping("info")
    public AjaxResult info(@NotBlank(message = "合格证信息ID不能为空！") String certId){
        // 合格证信息
        QualifiedCertDetailInfoVo certInfo = tbQualifiedCertInfoService.appGetQualifiedCertInfo(certId);
        if(certInfo == null) {
            throw new ServiceException("合格证数据不存在！");
        }
        // 主体信息
        Map<String, Object> entityInfo = tbUserEntityInfoService.appGetEntityInfo(certInfo.getEntityId());

        Map<String, Object> result = new HashMap<>();
        certInfo.setEntityId(null);
        result.put("certInfo", certInfo);
        result.put("entityInfo", entityInfo);
        return AjaxResult.success(result);
    }

}
